package org.jacp.demo.ui.fw.components;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jacp.demo.entities.Employee;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;

import com.explodingpixels.macwidgets.HudWidgetFactory;
import java.awt.FlowLayout;

public class EmployeeForm extends JPanel {

	private Employee employee = new Employee();
	private JLabel lbl_firstname;
	private JLabel lbl_lastname;
	private JLabel lbl_street;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel image_Label;

	public EmployeeForm() {
		setLayout(new BorderLayout(0, 0));

		JPanel imagePanel = new JPanel();
		add(imagePanel, BorderLayout.NORTH);
		imagePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		image_Label = HudWidgetFactory.createHudLabel("");
		imagePanel.add(image_Label);

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[] { 5, 75, 35, 37, 5, 0 };
		gbl_panel_1.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 15, 0, 0, 0 };
		gbl_panel_1.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel_1.setLayout(gbl_panel_1);

		lbl_firstname = HudWidgetFactory.createHudLabel(" ");
		GridBagConstraints gbc_lbl_firstname = new GridBagConstraints();
		gbc_lbl_firstname.anchor = GridBagConstraints.WEST;
		gbc_lbl_firstname.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_firstname.gridx = 1;
		gbc_lbl_firstname.gridy = 0;
		panel_1.add(lbl_firstname, gbc_lbl_firstname);

		lbl_lastname = HudWidgetFactory.createHudLabel(" ");
		GridBagConstraints gbc_lbl_lastname = new GridBagConstraints();
		gbc_lbl_lastname.gridwidth = 2;
		gbc_lbl_lastname.anchor = GridBagConstraints.WEST;
		gbc_lbl_lastname.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_lastname.gridx = 1;
		gbc_lbl_lastname.gridy = 1;
		panel_1.add(lbl_lastname, gbc_lbl_lastname);

		lbl_street = HudWidgetFactory.createHudLabel(" ");
		GridBagConstraints gbc_lbl_street = new GridBagConstraints();
		gbc_lbl_street.anchor = GridBagConstraints.WEST;
		gbc_lbl_street.gridwidth = 2;
		gbc_lbl_street.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_street.gridx = 1;
		gbc_lbl_street.gridy = 3;
		panel_1.add(lbl_street, gbc_lbl_street);

		label_2 = HudWidgetFactory.createHudLabel(" ");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 3;
		gbc_label_2.gridy = 3;
		panel_1.add(label_2, gbc_label_2);

		JLabel label_1 = HudWidgetFactory.createHudLabel("10130");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.anchor = GridBagConstraints.WEST;
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 1;
		gbc_label_1.gridy = 4;
		panel_1.add(label_1, gbc_label_1);

		JLabel lblBerlin = HudWidgetFactory.createHudLabel("Berlin");
		GridBagConstraints gbc_lblBerlin = new GridBagConstraints();
		gbc_lblBerlin.anchor = GridBagConstraints.WEST;
		gbc_lblBerlin.gridwidth = 2;
		gbc_lblBerlin.insets = new Insets(0, 0, 5, 5);
		gbc_lblBerlin.gridx = 2;
		gbc_lblBerlin.gridy = 4;
		panel_1.add(lblBerlin, gbc_lblBerlin);

		JLabel lblGermany = HudWidgetFactory.createHudLabel("GERMANY");
		GridBagConstraints gbc_lblGermany = new GridBagConstraints();
		gbc_lblGermany.anchor = GridBagConstraints.WEST;
		gbc_lblGermany.gridwidth = 3;
		gbc_lblGermany.insets = new Insets(0, 0, 5, 5);
		gbc_lblGermany.gridx = 1;
		gbc_lblGermany.gridy = 5;
		panel_1.add(lblGermany, gbc_lblGermany);

		label_3 = HudWidgetFactory.createHudLabel(" ");
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.insets = new Insets(0, 0, 5, 5);
		gbc_label_3.gridx = 1;
		gbc_label_3.gridy = 7;
		panel_1.add(label_3, gbc_label_3);

		JLabel lblEur = HudWidgetFactory.createHudLabel("EUR");
		GridBagConstraints gbc_lblEur = new GridBagConstraints();
		gbc_lblEur.insets = new Insets(0, 0, 5, 5);
		gbc_lblEur.gridx = 2;
		gbc_lblEur.gridy = 7;
		panel_1.add(lblEur, gbc_lblEur);
		init();
		initDataBindings();
	}

	private BufferedImage getImageBySex(String sex) {
		sex = sex == null ? "" : sex;
		System.out.println("#####" + sex + "####" + sex.length());
		BufferedImage image = null;
		try {
			String file = "F".equals(sex) ? "woman.jpg"
					: "M".equals(sex) ? "man.jpg" : "empty.jpg";
			image = ImageIO.read(new File("./data/" + file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;

	}

	public void clear() {
		setEmployee(new Employee());
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
		init();
	}

	private void init() {
		image_Label
				.setIcon(new ImageIcon(getImageBySex("" + employee.getSex())));
	}

	protected void initDataBindings() {
		BeanProperty<Employee, String> employeeBeanProperty = BeanProperty
				.create("firstName");
		BeanProperty<JLabel, String> jLabelBeanProperty = BeanProperty
				.create("text");
		AutoBinding<Employee, String, JLabel, String> autoBinding = Bindings
				.createAutoBinding(UpdateStrategy.READ, employee,
						employeeBeanProperty, lbl_firstname,
						jLabelBeanProperty, "firstName");
		autoBinding.bind();
		//
		BeanProperty<Employee, String> employeeBeanProperty_1 = BeanProperty
				.create("lastName");
		AutoBinding<Employee, String, JLabel, String> autoBinding_1 = Bindings
				.createAutoBinding(UpdateStrategy.READ, employee,
						employeeBeanProperty_1, lbl_lastname,
						jLabelBeanProperty, "lastName");
		autoBinding_1.bind();
		//
		BeanProperty<Employee, String> employeeBeanProperty_2 = BeanProperty
				.create("addr.street");
		AutoBinding<Employee, String, JLabel, String> autoBinding_2 = Bindings
				.createAutoBinding(UpdateStrategy.READ, employee,
						employeeBeanProperty_2, lbl_street, jLabelBeanProperty,
						"street");
		autoBinding_2.bind();
		//
		BeanProperty<Employee, Integer> employeeBeanProperty_3 = BeanProperty
				.create("addr.number");
		AutoBinding<Employee, Integer, JLabel, String> autoBinding_3 = Bindings
				.createAutoBinding(UpdateStrategy.READ, employee,
						employeeBeanProperty_3, label_2, jLabelBeanProperty,
						"number");
		autoBinding_3.bind();
		//
		BeanProperty<Employee, Double> employeeBeanProperty_4 = BeanProperty
				.create("salary");
		AutoBinding<Employee, Double, JLabel, String> autoBinding_4 = Bindings
				.createAutoBinding(UpdateStrategy.READ, employee,
						employeeBeanProperty_4, label_3, jLabelBeanProperty,
						"salary");
		autoBinding_4.bind();
	}
}
